package defaultSysConfig

import (
	"fmt"
	"saas_sys_base/common"
	. "saas_sys_base/modules/system/models"
	. "saas_sys_base/modules/system/service"
	"strconv"
	"strings"

	. "saas_sys_base/modules/adminSaas/models"
	. "saas_sys_base/modules/adminSaas/service"
)

func SetDefaultData() error {

	var sysUserService SysUserService

	user, _ := sysUserService.GetSysUserByAccount("admin")
	if user != nil {
		return nil
	}

	_, total, _ := sysUserService.ListSysUser("", 0, 10)
	if total > 0 {
		return nil
	}

	/*
			默认菜单：
			运营管理
		        用户管理
			配置管理

			系统管理
			    系统用户
			    角色管理
			    菜单管理
	*/

	//加入默认菜单
	//先不考虑路径前缀的问题 Config.Code.Prjprex

	menulist := G_SysMenulistDefault

	var sysMenuService SysMenuService
	var menuIds []string
	var parentId int64 = 0
	for _, menu := range menulist {

		if menu.Pid == -1 {
			menu.Pid = parentId
		}
		sysMenuService.SaveSysMenu(&menu)

		if menu.Pid == 0 {
			parentId = menu.Id
		}

		menuIds = append(menuIds, strconv.FormatInt(menu.Id, 10))
	}
	menuIdStr := strings.Replace(strings.Trim(fmt.Sprint(menuIds), "[]"), " ", ",", -1)

	/*var tbSysMenu SysMenu
	var tbSysMenuParent SysMenu
	tbSysMenuParent.Name = "配置管理"
	tbSysMenuParent.MType = 1
	tbSysMenuParent.Pid = 0
	tbSysMenuParent.Show = 1

	var sysMenuService SysMenuService
	sysMenuService.SaveSysMenu(&tbSysMenuParent)

	tbSysMenu.Name = "配置菜单"
	tbSysMenu.MType = 0
	tbSysMenu.Pid = tbSysMenuParent.Id
	tbSysMenu.Show = 1
	tbSysMenu.Url = "/admin/SysMenu/listpage"
	sysMenuService.SaveSysMenu(&tbSysMenu)*/

	//加入角色
	var tbSysRole SysRole
	var sysRoleService SysRoleService
	tbSysRole.Name = "admin"
	tbSysRole.Brief = "管理员"
	//tbSysRole.AddRecode(&tbSysRole, strconv.FormatInt(tbSysMenu.Id, 10))
	//sysRoleService.SaveSysRole(&tbSysRole, fmt.Sprintf("%s,%s", strconv.FormatInt(tbSysMenu.Id, 10), strconv.FormatInt(tbSysMenuParent.Id, 10)))
	sysRoleService.SaveSysRole(&tbSysRole, menuIdStr)

	//加入系统管理用户
	var sysUser SysUser
	var sysUserProfile SysUserProfile
	sysUser.Account = "admin"
	sysUser.Password = "c92df2206dd43fa8dfa9bcb2da79d076" //对应明文123456
	sysUser.Status = 0

	sysUserProfile.RoleId = tbSysRole.Id
	// sysUserProfile.SysUid = sysUser.Id 内部函数配置了这个关系
	sysUserService.SaveSysUser(&sysUser, &sysUserProfile)

	return nil
}

func ConfigSysMenuFromConf() {
	ConfigSysMenu(G_SysMenulistDefault)
}


func ConfigSysMenu(SysMenuList []SysMenu) {
	CheckMenuConfig(SysMenuList)
	menulist := SysMenuList

	var sysMenuService SysMenuService
	//var menuIds []string
	//var parentMenuConf *SysMenu
	var parentId int64 = 0
	var parentName string
	var currentParentName *string
	var currentMenu *SysMenu
	for _, menuConf := range menulist {
		if menuConf.Pid == 0 {
			parentName = menuConf.Name
			currentParentName = nil
		} else {
			currentParentName = &parentName
			menuConf.Pid = parentId
		}

		menuDb, _ := sysMenuService.GetMenuByName(&menuConf, currentParentName)
		if  menuDb == nil {
			sysMenuService.SaveSysMenu(&menuConf)
			currentMenu = &menuConf
		} else {
			currentMenu = menuDb
		}


		if menuConf.Pid == 0 { //该菜单为父菜单时，更新父菜单游标
			parentId = currentMenu.Id

		}

		//menuIds = append(menuIds, strconv.FormatInt(menu.Id, 10))
	}
}

func ConfigTenantMenuFromConf() {
	ConfigTenantMenu(G_AppMenulistDefault)
}

func ConfigTenantMenu(menuList []AdminSaasMenu) {
	menulist := menuList
	var adminMenuService AdminSaasMenuService

	adminMenuService.CheckMenuConfig(menuList)
	//var menuIds []string
	//var parentMenuConf *SysMenu
	var parentId int64 = 0
	var parentName string
	var currentParentName *string
	var currentMenu *AdminSaasMenu
	for _, menuConf := range menulist {
		appwho := common.NewWho(0, menuConf.MiniAppId, 0)

		if menuConf.Pid == 0 {
			parentName = menuConf.Name
			currentParentName = nil
		} else {
			currentParentName = &parentName
			menuConf.Pid = parentId
		}

		menuDb, _ := adminMenuService.GetMenuByName(appwho, &menuConf, currentParentName)
		if  menuDb == nil {
			adminMenuService.SaveAdminSaasMenu(&menuConf)
			currentMenu = &menuConf
		} else {
			currentMenu = menuDb
		}


		if menuConf.Pid == 0 { //该菜单为父菜单时，更新父菜单游标
			parentId = currentMenu.Id

		}

		//menuIds = append(menuIds, strconv.FormatInt(menu.Id, 10))
	}
}

func ConfigDefault() {
	SetDefaultData()
	ConfigSysMenuFromConf()
	ConfigTenantMenuFromConf()

	_ = ConfigAppSuit()
}